<?xml version="1.0" encoding="iso-8859-1"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  <html xmlns="http://www.w3.org/1999/xhtml">
<head>
	<title>Working with SPARQL queries</title>
	<link rel="stylesheet" type="text/css" href="../media/style.css">
	<meta http-equiv='Content-Type' content='text/html; charset=iso-8859-1'/>
	<script src="media/a.js"></script>
</head>
<body>

<table border="0" cellspacing="0" cellpadding="0" height="48" width="100%">
  <tr>
	<td class="header-top-left"><img src="../media/logo.png" border="0" alt="phpDocumentor " /></td>
    <td class="header-top-right">QueryPath<br /><div class="header-top-right-subpackage"></div></td>
  </tr>
  <tr><td colspan="2" class="header-line"><img src="../media/empty.png" width="1" height="1" border="0" alt=""  /></td></tr>
  <tr>
    <td colspan="2" class="header-menu">
      
                        
                                                  		[ <a href="../classtrees_QueryPath.html" class="menu">class tree: QueryPath</a> ]
		[ <a href="../elementindex_QueryPath.html" class="menu">index: QueryPath</a> ]
            [ <a href="../elementindex.html" class="menu">all elements</a> ]
    </td>
  </tr>
  <tr><td colspan="2" class="header-line"><img src="../media/empty.png" width="1" height="1" border="0" alt=""  /></td></tr>
</table>

<table width="100%" border="0" cellpadding="0" cellspacing="0">
  <tr valign="top">
    <td width="195" class="menu">
		<div class="package-title">QueryPath</div>
  <div class="package">
	<div id="todolist">
			<p><a href="../todolist.html">Todo List</a></p>
	</div>
	</div>
      <b>Packages:</b><br />
  <div class="package">
              <a href="../li_QueryPath.html">QueryPath</a><br />
              <a href="../li_Examples.html">Examples</a><br />
              <a href="../li_Tests.html">Tests</a><br />
      	</div>
      <br />
                        <br />
                      </td>
    <td>
      <table cellpadding="10" cellspacing="0" width="100%" border="0"><tr><td valign="top">

<h1 align="center">Working with SPARQL queries</h1>
<div class="src-code"><span class="php">
<ol><li><div class="src-line"><span class="src-php">&lt;?php</span></div></li>
<li><div class="src-line"><span class="src-doc">/**</span></div></li>
<li><div class="src-line"><span class="src-doc">&nbsp;*&nbsp;Use&nbsp;QueryPath&nbsp;to&nbsp;query&nbsp;SPARQL&nbsp;endpoints&nbsp;on&nbsp;semantic&nbsp;web&nbsp;servers.</span></div></li>
<li><div class="src-line"><span class="src-doc">&nbsp;*</span></div></li>
<li><div class="src-line"><span class="src-doc">&nbsp;*&nbsp;This&nbsp;example&nbsp;runs&nbsp;a&nbsp;SPARQL&nbsp;query&nbsp;against&nbsp;a&nbsp;remote&nbsp;database&nbsp;server</span></div></li>
<li><div class="src-line"><span class="src-doc">&nbsp;*&nbsp;(</span><span class="src-doc-inlinetag">{@link&nbsp;http://dbpedia.org}</span><span class="src-doc">)&nbsp;and&nbsp;then&nbsp;parses&nbsp;the&nbsp;returned&nbsp;XML&nbsp;data,</span></div></li>
<li><div class="src-line"><span class="src-doc">&nbsp;*&nbsp;displaying&nbsp;it&nbsp;as&nbsp;an&nbsp;HTML&nbsp;table.</span></div></li>
<li><div class="src-line"><span class="src-doc">&nbsp;*</span></div></li>
<li><div class="src-line"><span class="src-doc">&nbsp;*&nbsp;This&nbsp;demo&nbsp;shows&nbsp;how&nbsp;a&nbsp;more&nbsp;complex&nbsp;GET&nbsp;query&nbsp;can&nbsp;be&nbsp;built&nbsp;up&nbsp;in</span></div></li>
<li><div class="src-line"><span class="src-doc">&nbsp;*&nbsp;QueryPath.&nbsp;POST&nbsp;queries&nbsp;are&nbsp;supported,&nbsp;too.&nbsp;Use&nbsp;a&nbsp;stream&nbsp;context</span></div></li>
<li><div class="src-line"><span class="src-doc">&nbsp;*&nbsp;to&nbsp;create&nbsp;those.</span></div></li>
<li><div class="src-line"><span class="src-doc">&nbsp;*</span></div></li>
<li><div class="src-line"><span class="src-doc">&nbsp;*&nbsp;</span><span class="src-doc-coretag">@package</span><span class="src-doc">&nbsp;Examples</span></div></li>
<li><div class="src-line"><span class="src-doc">&nbsp;*&nbsp;</span><span class="src-doc-coretag">@author</span><span class="src-doc">&nbsp;M&nbsp;Butcher&nbsp;&lt;matt@aleph-null.tv&gt;</span></div></li>
<li><div class="src-line"><span class="src-doc">&nbsp;*&nbsp;</span><span class="src-doc-coretag">@license</span><span class="src-doc">&nbsp;LGPL&nbsp;The&nbsp;GNU&nbsp;Lesser&nbsp;GPL&nbsp;(LGPL)&nbsp;or&nbsp;an&nbsp;MIT-like&nbsp;license.</span></div></li>
<li><div class="src-line"><span class="src-doc">&nbsp;*&nbsp;</span><span class="src-doc-coretag">@see</span><span class="src-doc">&nbsp;http://www.w3.org/2009/sparql/wiki/Main_Page</span></div></li>
<li><div class="src-line"><span class="src-doc">&nbsp;*&nbsp;</span><span class="src-doc-coretag">@see</span><span class="src-doc">&nbsp;http://dbpedia.org</span></div></li>
<li><div class="src-line"><span class="src-doc">&nbsp;*&nbsp;</span><span class="src-doc-coretag">@see</span><span class="src-doc">&nbsp;dbpedia.php</span></div></li>
<li><div class="src-line"><span class="src-doc">&nbsp;*&nbsp;</span><span class="src-doc-coretag">@see</span><span class="src-doc">&nbsp;musicbrainz.php</span></div></li>
<li><div class="src-line"><span class="src-doc">&nbsp;*&nbsp;</span><span class="src-doc-coretag">@see</span><span class="src-doc">&nbsp;http://drupal.org/project/querypath</span></div></li>
<li><div class="src-line"><span class="src-doc">&nbsp;*/</span></div></li>
<li><div class="src-line">&nbsp;</div></li>
<li><div class="src-line">require&nbsp;<span class="src-str">'../src/QueryPath/QueryPath.php'</span><span class="src-sym">;</span></div></li>
<li><div class="src-line">&nbsp;</div></li>
<li><div class="src-line"><span class="src-comm">//&nbsp;We&nbsp;are&nbsp;using&nbsp;the&nbsp;dbpedia&nbsp;database&nbsp;to&nbsp;execute&nbsp;a&nbsp;SPARQL&nbsp;query.</span></div></li>
<li><div class="src-line">&nbsp;</div></li>
<li><div class="src-line"><span class="src-comm">//&nbsp;URL&nbsp;to&nbsp;DB&nbsp;Pedia's&nbsp;SPARQL&nbsp;endpoint.</span></div></li>
<li><div class="src-line"><span class="src-var">$url&nbsp;</span>=&nbsp;<span class="src-str">'http://dbpedia.org/sparql'</span><span class="src-sym">;</span></div></li>
<li><div class="src-line">&nbsp;</div></li>
<li><div class="src-line"><span class="src-comm">//&nbsp;The&nbsp;SPARQL&nbsp;query&nbsp;to&nbsp;run.</span></div></li>
<li><div class="src-line"><span class="src-var">$sparql&nbsp;</span>=&nbsp;<span class="src-str">'</span></div></li>
<li><div class="src-line"><span class="src-str">&nbsp;&nbsp;PREFIX&nbsp;foaf:&nbsp;&lt;http://xmlns.com/foaf/0.1/&gt;</span></div></li>
<li><div class="src-line"><span class="src-str">&nbsp;&nbsp;PREFIX&nbsp;rdfs:&nbsp;&lt;http://www.w3.org/2000/01/rdf-schema#&gt;</span></div></li>
<li><div class="src-line"><span class="src-str">&nbsp;&nbsp;SELECT&nbsp;?uri&nbsp;?name&nbsp;?label</span></div></li>
<li><div class="src-line"><span class="src-str">&nbsp;&nbsp;WHERE&nbsp;{</span></div></li>
<li><div class="src-line"><span class="src-str">&nbsp;&nbsp;&nbsp;&nbsp;?uri&nbsp;foaf:name&nbsp;?name&nbsp;.</span></div></li>
<li><div class="src-line"><span class="src-str">&nbsp;&nbsp;&nbsp;&nbsp;?uri&nbsp;rdfs:label&nbsp;?label</span></div></li>
<li><div class="src-line"><span class="src-str">&nbsp;&nbsp;&nbsp;&nbsp;FILTER&nbsp;(?name&nbsp;=&nbsp;&quot;The&nbsp;Beatles&quot;)</span></div></li>
<li><div class="src-line"><span class="src-str">&nbsp;&nbsp;&nbsp;&nbsp;FILTER&nbsp;(lang(?label)&nbsp;=&nbsp;&quot;en&quot;)</span></div></li>
<li><div class="src-line"><span class="src-str">&nbsp;&nbsp;}</span></div></li>
<li><div class="src-line"><span class="src-str">'</span><span class="src-sym">;</span></div></li>
<li><div class="src-line">&nbsp;</div></li>
<li><div class="src-line"><span class="src-comm">//&nbsp;We&nbsp;first&nbsp;set&nbsp;up&nbsp;the&nbsp;parameters&nbsp;that&nbsp;will&nbsp;be&nbsp;sent.</span></div></li>
<li><div class="src-line"><span class="src-var">$params&nbsp;</span>=&nbsp;<span class="src-key">array</span><span class="src-sym">(</span></div></li>
<li><div class="src-line">&nbsp;&nbsp;<span class="src-str">'query'&nbsp;</span>=&gt;&nbsp;<span class="src-var">$sparql</span><span class="src-sym">,</span></div></li>
<li><div class="src-line">&nbsp;&nbsp;<span class="src-str">'format'&nbsp;</span>=&gt;&nbsp;<span class="src-str">'application/sparql-results+xml'</span><span class="src-sym">,</span></div></li>
<li><div class="src-line"><span class="src-sym">)</span><span class="src-sym">;</span></div></li>
<li><div class="src-line">&nbsp;</div></li>
<li><div class="src-line"><span class="src-comm">//&nbsp;DB&nbsp;Pedia&nbsp;wants&nbsp;a&nbsp;GET&nbsp;query,&nbsp;so&nbsp;we&nbsp;create&nbsp;one.</span></div></li>
<li><div class="src-line"><span class="src-var">$data&nbsp;</span>=&nbsp;<a href="http://www.php.net/http_build_query">http_build_query</a><span class="src-sym">(</span><span class="src-var">$params</span><span class="src-sym">)</span><span class="src-sym">;</span></div></li>
<li><div class="src-line"><span class="src-var">$url&nbsp;</span>.=&nbsp;<span class="src-str">'?'&nbsp;</span>.&nbsp;<span class="src-var">$data</span><span class="src-sym">;</span></div></li>
<li><div class="src-line">&nbsp;</div></li>
<li><div class="src-line"><span class="src-comm">//&nbsp;Next,&nbsp;we&nbsp;simply&nbsp;retrieve,&nbsp;parse,&nbsp;and&nbsp;output&nbsp;the&nbsp;contents.</span></div></li>
<li><div class="src-line"><span class="src-var">$qp&nbsp;</span>=&nbsp;<a href="../QueryPath/_src---QueryPath---QueryPath.php.html#functionqp">qp</a><span class="src-sym">(</span><span class="src-var">$url</span><span class="src-sym">,&nbsp;</span><span class="src-str">'head'</span><span class="src-sym">)</span><span class="src-sym">;</span></div></li>
<li><div class="src-line">&nbsp;</div></li>
<li><div class="src-line"><span class="src-comm">//&nbsp;Get&nbsp;the&nbsp;headers&nbsp;from&nbsp;the&nbsp;resulting&nbsp;XML.</span></div></li>
<li><div class="src-line"><span class="src-var">$headers&nbsp;</span>=&nbsp;<span class="src-key">array</span><span class="src-sym">(</span><span class="src-sym">)</span><span class="src-sym">;</span></div></li>
<li><div class="src-line"><span class="src-key">foreach&nbsp;</span><span class="src-sym">(</span><span class="src-var">$qp</span><span class="src-sym">-&gt;</span><span class="src-id">children</span><span class="src-sym">(</span><span class="src-str">'variable'</span><span class="src-sym">)&nbsp;</span><span class="src-key">as&nbsp;</span><span class="src-var">$col</span><span class="src-sym">)&nbsp;</span><span class="src-sym">{</span></div></li>
<li><div class="src-line">&nbsp;&nbsp;<span class="src-var">$headers</span><span class="src-sym">[</span><span class="src-sym">]&nbsp;</span>=&nbsp;<span class="src-var">$col</span><span class="src-sym">-&gt;</span><span class="src-id">attr</span><span class="src-sym">(</span><span class="src-str">'name'</span><span class="src-sym">)</span><span class="src-sym">;</span></div></li>
<li><div class="src-line"><span class="src-sym">}</span></div></li>
<li><div class="src-line">&nbsp;</div></li>
<li><div class="src-line"><span class="src-comm">//&nbsp;Get&nbsp;rows&nbsp;of&nbsp;data&nbsp;from&nbsp;result.</span></div></li>
<li><div class="src-line"><span class="src-var">$rows&nbsp;</span>=&nbsp;<span class="src-key">array</span><span class="src-sym">(</span><span class="src-sym">)</span><span class="src-sym">;</span></div></li>
<li><div class="src-line"><span class="src-var">$col_count&nbsp;</span>=&nbsp;<a href="http://www.php.net/count">count</a><span class="src-sym">(</span><span class="src-var">$headers</span><span class="src-sym">)</span><span class="src-sym">;</span></div></li>
<li><div class="src-line"><span class="src-key">foreach&nbsp;</span><span class="src-sym">(</span><span class="src-var">$qp</span><span class="src-sym">-&gt;</span><span class="src-id">top</span><span class="src-sym">(</span><span class="src-sym">)</span><span class="src-sym">-&gt;</span><span class="src-id">find</span><span class="src-sym">(</span><span class="src-str">'results&gt;result'</span><span class="src-sym">)&nbsp;</span><span class="src-key">as&nbsp;</span><span class="src-var">$row</span><span class="src-sym">)&nbsp;</span><span class="src-sym">{</span></div></li>
<li><div class="src-line">&nbsp;&nbsp;<span class="src-var">$cols&nbsp;</span>=&nbsp;<span class="src-key">array</span><span class="src-sym">(</span><span class="src-sym">)</span><span class="src-sym">;</span></div></li>
<li><div class="src-line">&nbsp;&nbsp;<span class="src-var">$row</span><span class="src-sym">-&gt;</span><span class="src-id">children</span><span class="src-sym">(</span><span class="src-sym">)</span><span class="src-sym">;</span></div></li>
<li><div class="src-line">&nbsp;&nbsp;<span class="src-key">for&nbsp;</span><span class="src-sym">(</span><span class="src-var">$i&nbsp;</span>=&nbsp;<span class="src-num">0</span><span class="src-sym">;&nbsp;</span><span class="src-var">$i&nbsp;</span>&lt;&nbsp;<span class="src-var">$col_count</span><span class="src-sym">;&nbsp;</span>++<span class="src-var">$i</span><span class="src-sym">)&nbsp;</span><span class="src-sym">{</span></div></li>
<li><div class="src-line">&nbsp;&nbsp;&nbsp;&nbsp;<span class="src-var">$cols</span><span class="src-sym">[</span><span class="src-var">$i</span><span class="src-sym">]&nbsp;</span>=&nbsp;<span class="src-var">$row</span><span class="src-sym">-&gt;</span><span class="src-id">branch</span><span class="src-sym">(</span><span class="src-sym">)</span><span class="src-sym">-&gt;</span><span class="src-id">eq</span><span class="src-sym">(</span><span class="src-var">$i</span><span class="src-sym">)</span><span class="src-sym">-&gt;</span><span class="src-id">text</span><span class="src-sym">(</span><span class="src-sym">)</span><span class="src-sym">;</span></div></li>
<li><div class="src-line">&nbsp;&nbsp;<span class="src-sym">}</span></div></li>
<li><div class="src-line">&nbsp;&nbsp;<span class="src-var">$rows</span><span class="src-sym">[</span><span class="src-sym">]&nbsp;</span>=&nbsp;<span class="src-var">$cols</span><span class="src-sym">;</span></div></li>
<li><div class="src-line"><span class="src-sym">}</span></div></li>
<li><div class="src-line">&nbsp;</div></li>
<li><div class="src-line"><span class="src-comm">//&nbsp;Turn&nbsp;data&nbsp;into&nbsp;table.</span></div></li>
<li><div class="src-line"><span class="src-var">$table&nbsp;</span>=&nbsp;<span class="src-str">'&lt;table&gt;&lt;tr&gt;&lt;th&gt;'&nbsp;</span>.&nbsp;<a href="http://www.php.net/implode">implode</a><span class="src-sym">(</span><span class="src-str">'&lt;/th&gt;&lt;th&gt;'</span><span class="src-sym">,&nbsp;</span><span class="src-var">$headers</span><span class="src-sym">)&nbsp;</span>.&nbsp;<span class="src-str">'&lt;/th&gt;&lt;/tr&gt;'</span><span class="src-sym">;</span></div></li>
<li><div class="src-line"><span class="src-key">foreach&nbsp;</span><span class="src-sym">(</span><span class="src-var">$rows&nbsp;</span><span class="src-key">as&nbsp;</span><span class="src-var">$row</span><span class="src-sym">)&nbsp;</span><span class="src-sym">{</span></div></li>
<li><div class="src-line">&nbsp;&nbsp;<span class="src-var">$table&nbsp;</span>.=&nbsp;<span class="src-str">'&lt;tr&gt;&lt;td&gt;'</span><span class="src-sym">;</span></div></li>
<li><div class="src-line">&nbsp;&nbsp;<span class="src-var">$table&nbsp;</span>.=&nbsp;<a href="http://www.php.net/implode">implode</a><span class="src-sym">(</span><span class="src-str">'&lt;/td&gt;&lt;td&gt;'</span><span class="src-sym">,&nbsp;</span><span class="src-var">$row</span><span class="src-sym">)</span><span class="src-sym">;</span></div></li>
<li><div class="src-line">&nbsp;&nbsp;<span class="src-var">$table&nbsp;</span>.=&nbsp;<span class="src-str">'&lt;/td&gt;&lt;/tr&gt;'</span><span class="src-sym">;</span></div></li>
<li><div class="src-line"><span class="src-sym">}</span></div></li>
<li><div class="src-line"><span class="src-var">$table&nbsp;</span>.=&nbsp;<span class="src-str">'&lt;/table&gt;'</span><span class="src-sym">;</span></div></li>
<li><div class="src-line">&nbsp;</div></li>
<li><div class="src-line"><span class="src-comm">//&nbsp;Add&nbsp;table&nbsp;to&nbsp;HTML&nbsp;document.</span></div></li>
<li><div class="src-line"><a href="../QueryPath/_src---QueryPath---QueryPath.php.html#functionqp">qp</a><span class="src-sym">(</span><span class="src-id"><a href="../QueryPath/QueryPath.html">QueryPath</a></span><span class="src-sym">::</span><span class="src-id">HTML_STUB</span><span class="src-sym">,&nbsp;</span><span class="src-str">'body'</span><span class="src-sym">)</span><span class="src-sym">-&gt;</span><span class="src-id">append</span><span class="src-sym">(</span><span class="src-var">$table</span><span class="src-sym">)</span><span class="src-sym">-&gt;</span><span class="src-id">writeHTML</span><span class="src-sym">(</span><span class="src-sym">)</span><span class="src-sym">;</span></div></li>
</ol>
</span></div>
        <div class="credit">
		    <hr class="separator" />
		    Documentation generated on Thu, 19 Mar 2009 10:21:11 -0500 by <a href="http://www.phpdoc.org">phpDocumentor 1.4.2</a>
	      </div>
      </td></tr></table>
    </td>
  </tr>
</table>

</body>
</html>